package com.example.coursemanagement.controller;


import com.example.coursemanagement.MyUtils.CommonResult;
import com.example.coursemanagement.MyUtils.ResultCode;
import com.example.coursemanagement.mapper.QuestionMapper;
import com.example.coursemanagement.pojo.Question;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;


import java.util.Collection;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author yangmenghen
 * @since 2020-06-01
 */
@RestController
@RequestMapping("/question" )
@RequiredArgsConstructor
public class QuestionController{

    private final QuestionMapper questionMapper;

    @ApiOperation("查询所有题库")
    @RequestMapping(value = "/selectAll",method = RequestMethod.GET)
    public CommonResult<Object> queryAllQuestion(){
        Collection<Question> questions = questionMapper.selectList(null);
        if(questions==null){
            return CommonResult.failed(ResultCode.FAILED);
        }
        else{
            return CommonResult.success(questions);
        }

    }

    @ApiOperation("录题")
    @RequestMapping(value = "/addQuestion",method = RequestMethod.POST)
    public CommonResult<Object> addQuestion(Question question){
        if(questionMapper.insert(question)>0){
            return CommonResult.success(ResultCode.ADD_SUCCESS);
        }
        else{
            return CommonResult.failed(ResultCode.FAILED);
        }

    }

    @ApiOperation("删除题库")
    @RequestMapping(value = "/updateQuestion",method = RequestMethod.DELETE)
    public CommonResult<Object> deleteQuestion(Question question){
        if(questionMapper.deleteById(question)>0){
            return CommonResult.success(ResultCode.DELETE_SUCCESS);
        }
        else{
            return CommonResult.failed(ResultCode.FAILED);
        }
    }

}

